<?php 
    session_start();
//    echo "Login: ".$_SESSION["userID"]." ".$_SESSION["userType"];
?>

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        // put your code here
        if(isset($_SESSION["userID"]) && !strcmp($_SESSION["userType"], "seller")){
                 
        $button = $_POST["button"];    
        $saleID = $_POST["saleID"];
        $productID = $_POST["productID"];
        $discount = $_POST["discount"];
        $startDate = $_POST["startDate"];
        $endDate = $_POST["endDate"];
        
        if(strcmp($button, "Cancel")){
        $con = mysql_connect("localhost", "zhouzhao", "19831022");
        if(!$con){
            die("could not connect to DB: ".mysql_error());
        }
        
        mysql_selectdb("cloudcom", $con);
        
        if(strlen($saleID) == 0){
            mysql_query("insert into sales values
            (null, '$productID', $discount, '$startDate', '$endDate')");
//            echo "<p style='color:red'>new sale $saleID is inserted</p>";
        }else{
            mysql_query("update sales set productID='$productID', discount=$discount, startDate='$startDate', endDate='$endDate' where saleID=$saleID");
//            echo "<p style='color:red'>sale $saleID is updated</p>";
        }
        
        mysql_close($con);
        
        require 'seller.php';
        }else{
            require 'seller.php';
        }
        
        }else{
            require 'login.html';
        }
        ?>
    </body>
</html>
